﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Text.RegularExpressions;
using System.Windows;
using System.Web;

// GetTPB, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
namespace GetTpb
{
    public class ParsePage
    {
        //public string regexTorrents { get; set; }
        //public string regexTorrent { get; set; }
        //public string regexTParams { get; set; }
        //public string regexNext { get; set; }
        //
        //private Regex r = new Regex("<div class=\"detName\">(.*?)</a></div>", RegexOptions.Singleline | RegexOptions.IgnoreCase|RegexOptions.);
        //private Regex rTorrents = new Regex("<div class=\"detName\">(.*?)></font>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        //private Regex rTorrents = new Regex("<div class=\"detName\">(.*?)></font>[ \t\n]*</td>[ \t\n]*<td align=\"right\">[0-9]*</td>[ \t\n]*<td align=\"right\">[0-9]*</td>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        //private Regex rTorrents = new Regex("<div class=\"detName\"><a href=\"(.*?)\" class=\"detLink\" title=\"(.*?)\">.*?</a></div>[ ]*<a href=\"(.*?)\" title=\".*?\"><img src=\".*?\" alt=\"Magnet link\" /></a>[ ]*<a href=\"(.*?)\" title=\".*?\"><img src=\".*?\" class=\"dl\" alt=\"Download\" /></a><img src=\".*?\" /><img src=\".*?\" />[ ]*<font class=\"detDesc\">(.*?), Size (.*?), .*? <a class=\"detDesc\" href=\".*?\" title=\".*?\">.*?</a></font>[ ]*</td>[ ]*<td align=\"right\">([0-9]*)</td>[ ]*<td align=\"right\">([0-9]*)</td>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Regex rTorrents = new Regex("<tr>[ ]*<td class=\"vertTh\">(.*?)</tr>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private MatchCollection mTorrents;
        //
        //private Regex rTorrent = new Regex("<a href=\"(.*?)\" class=\"detLink\" title=\".*?\">(.*?)</a></div>[ \t\n]*<a href=\"(.*?)\" title=\"Download this torrent\"><img src=\".*?\" class=\"dl\" alt=\"Download\" /></a><a href=\"(.*?)\" title=\"Download this torrent using magnet\">.*?[ \t\n]*</td>[ \t\n]*<td align=\"right\">([0-9]*)</td>[ \t\n]*<td align=\"right\">([0-9]*)</td>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        //private Regex rTorrent = new Regex("<div class=\"detName\"><a href=\"(.*?)\" class=\"detLink\" title=\"(.*?)\">.*?</a></div>[ ]*<a href=\"(.*?)\" title=\".*?\"><img src=\".*?\" alt=\"Magnet link\" /></a>[ ]*<a href=\"(.*?)\" title=\".*?\"><img src=\".*?\" class=\"dl\" alt=\"Download\" /></a><img src=\".*?\" /><img src=\".*?\" />[ ]*<font class=\"detDesc\">(.*?), Size (.*?), .*? <a class=\"detDesc\" href=\".*?\" title=\".*?\">.*?</a></font>[ ]*</td>[ ]*<td align=\"right\">([0-9]*)</td>[ ]*<td align=\"right\">([0-9]*)</td>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        //private Match mTorrent;
        //private Regex rTParams = new Regex("Uploaded (.*?), Size (.*?), ULed by", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        //private Match mTParams;
        //
        //private Regex rNext = new Regex("&nbsp;<a href=\"([a-zA-Z0-9/]*?)\"><img src=\".*?\" border=\"0\" alt=\"Next\"/></a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        //private Regex rNext = new Regex("<a href=\"(/recent/[0-9/]*?)\"><img src=\"http://.*?\" border=\"0\" alt=\"Next\"/></a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        //<a href="/search/discography/2/99/100/"><img src="http://static.thepiratebay.se/img/next.gif" border="0" alt="Next"/></a>
        //private Regex rNext = new Regex("&nbsp;<a href=\"(/[a-zA-Z0-9/]*?)\"><img src=\"http://.*?\" border=\"0\" alt=\"Next\"/></a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

        //</a>&nbsp;<a href=\"/browse/202/1/3\"><img src=\"/static/img/next.gif\" border=\"0\" alt=\"Next\"/></a>&nbsp; 
        // 2013-4-14 zniknął podwójny // w obrazku
        private Regex rNext = new Regex("&nbsp;<a href=\"(/[a-zA-Z0-9/]*?)\"><img src=\".*?/.*?\" border=\"0\" alt=\"Next\"/></a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Match mNext;
        //
        public static Regex rSelection = null;
        //private static Match mSelection;

        //
        //
        public bool Do(string page, HashSet<TpbItem> hs, TpbSource ss, out string nextPage)
        {
            nextPage = "";
            //string torrentTitle, torrentPage, torrentTorrent, torrentMagnet, torrentUploaded, torrentSize, torrentSE, torrentLE;
            //
            page = page.Replace('\t', ' ');
            page = page.Replace('\n', ' ');
            page = page.Replace('\r', ' ');
            //
            page = Regex.Replace(page, "<script type=\"text/javascript\">.*?</script>", "", RegexOptions.Compiled| RegexOptions.IgnoreCase);
            //
            mNext = rNext.Match(page);
            if (2 == mNext.Groups.Count)
            {
                nextPage = mNext.Groups[1].Value;
            }

            //
            //
            mTorrents = rTorrents.Matches(page, 0);

            if (0 == mTorrents.Count)
            {
                return false;
            }
            //
            ParseOneRow por = new ParseOneRow();
            //
            for (int i = 0; i < mTorrents.Count; i++)
            {
                bool isOK = true;
                //
                string s = mTorrents[i].Value;
                if (por.Do(s))
                {
                    por.torrentPage = Utils.GetNextPage(ss.webPage, por.torrentPage);
                }
                else
                {
                    isOK = false;
                }
                //
                if (isOK)
                {
                    //
                    TpbItem ti = new TpbItem(por.torrentTitle, por.torrentPage, por.torrentTorrent, por.torrentMagnet, por.torrentUploaded, por.torrentSize, por.torrentSE, por.torrentLE);
                    if (0 != ss.minSize)
                    {
                        if (ti.Size < ss.minSize)
                        {
                            ;
                            int v = 0;
                            continue;
                        }
                    }
                    if (null != rSelection)
                    {
                        if (!rSelection.IsMatch(ti.Title))
                        {
                            ;
                            continue;
                        }
                    }
                    //
                    hs.Add(ti);
                    //
                }//isOK
                //
            }


            if ("" == nextPage)
            {
                int ccc = 0;
            }


            return true;
        }





    }//class

    internal class ParseOneRow
    {
        public string torrentTitle { get; set; }
        public string torrentPage { get; set; }
        public string torrentTorrent { get; set; }
        public string torrentMagnet { get; set; }
        public string torrentUploaded { get; set; }
        public string torrentSize { get; set; }
        public string torrentSE { get; set; }
        public string torrentLE { get; set; }
        //magnet"<a href=\"(magnet.*?)\" title="
        //
        //
        private static Regex rTitlePage = new Regex("<a href=\"([^ ]*?)\" class=\"detLink\" title=\"(.*?)\">", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Match mTitlePage;
        //
        private static Regex rTorrent = new Regex("<a href=\"(http://.*?.torrent)\" title=\"Download this torrent\">", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Match mTorrent;
        //
        private static Regex rMagnet = new Regex("<a href=\"(magnet.*?)\" title=\"Download this torrent using magnet\">", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Match mMagnet;
        //
        private static Regex rUploaded = new Regex("<font class=\"detDesc\">(.*?),", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Match mUploaded;
        //
        private static Regex rSize = new Regex("Size (.*?), ", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Match mSize;
        //
        private static Regex rSeLe = new Regex("<td align=\"right\">([0-9]*)</td>[ ]*<td align=\"right\">([0-9]*)</td>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
        private Match mSeLe;
        //
        public bool Do(string s)
        {
            torrentTitle = ""; torrentPage = ""; torrentTorrent = ""; torrentMagnet = ""; torrentUploaded = ""; torrentSize = ""; torrentSE = ""; torrentLE = "";
            //
            //
            mTitlePage = rTitlePage.Match(s);
            if (3 == mTitlePage.Groups.Count)
            {
                torrentPage = mTitlePage.Groups[1].Value.Trim();
                torrentTitle = mTitlePage.Groups[2].Value.Replace("Details for ", "").Trim();
                torrentTitle = HttpUtility.HtmlDecode(torrentTitle);
                if ("" == torrentTitle)
                {
                    int n = 0;
                }
            }
            else return false;
            //
            mTorrent = rTorrent.Match(s);
            if (1 == mTorrent.Groups.Count)
            {
                torrentTorrent = mTorrent.Groups[1].Value.Trim();
            }
            //else return false;
            //
            mMagnet = rMagnet.Match(s);
            if (2 == mMagnet.Groups.Count)
            {
                torrentMagnet = mMagnet.Groups[1].Value.Trim();
            }
            else return false;
            //
            mUploaded = rUploaded.Match(s);
            if (2 == mUploaded.Groups.Count)
            {
                torrentUploaded = mUploaded.Groups[1].Value.Replace("&nbsp;", " ").Replace("<b>", "").Replace("</b>", "").Trim();
            }
            else return false;
            //
            mSize = rSize.Match(s);
            if (2 == mSize.Groups.Count)
            {
                torrentSize = mSize.Groups[1].Value.Replace("&nbsp;", " ").Trim();
            }
            else return false;
            //
            mSeLe = rSeLe.Match(s);
            if (3 == mSeLe.Groups.Count)
            {
                torrentSE = mSeLe.Groups[1].Value.Trim();
                torrentLE = mSeLe.Groups[2].Value.Trim();
            }
            else return false;
            //
            //
            return true;
        }
        //
    }
}//namespace
